package com.tm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tm.dto.EmployeeDTO;
import com.tm.entity.EmployeeEntity;
import com.tm.service.EmployeeService;
import com.tm.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author huangp
 * @create 2023-04-05 9:36
 */
@RestController
@RequestMapping("employee")
@CrossOrigin
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @GetMapping("page")
    public Result getEmployeePage(@RequestParam(required = false,defaultValue = "1") Integer page,
                                 @RequestParam(required = false,defaultValue = "10") Integer pageSize,
                                 EmployeeDTO employeeDTO){
        Page<EmployeeEntity> page2 = new Page<>(page,pageSize);
        QueryWrapper<EmployeeEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(employeeDTO.getName()),"name",employeeDTO.getName());
        Page<EmployeeEntity> page1 = employeeService.page(page2,queryWrapper);
        return Result.success(page1);
    }

    @PostMapping
    public Result addEmployee(@RequestBody EmployeeEntity employeeEntity){
        Boolean flag = employeeService.addEmployee(employeeEntity);
        return Result.success(flag);
    }

    @GetMapping("/{id}")
    public Result queryEmployeeById(@PathVariable("id") Long id){
        EmployeeEntity employeeEntity = employeeService.getById(id);
        return Result.success(employeeEntity);
    }

    @DeleteMapping
    public Result batchDelete(@RequestParam("ids") List<Long> ids){
        Boolean flag = employeeService.removeByIds(ids);
        return Result.success(flag);
    }

    @PutMapping
    public Result editDish(@RequestBody EmployeeEntity employeeEntity ){
        UpdateWrapper<EmployeeEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq(employeeEntity.getId() != null,"id",employeeEntity.getId());
        return Result.success(employeeService.update(updateWrapper));
    }


}
